package com.CRApp.mapper;

import com.CRApp.pojo.Order;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 订单Mapper接口
 */
@Mapper
public interface OrderMapper {
    
    /**
     * 插入订单
     * @param order 订单对象
     * @return 影响的行数
     */
    @Insert("INSERT INTO orders(order_number, addresses_id, user_id, product_id, product_name, " +
            "quantity, product_price, total_price, status, created_at, recipient_name, phone, address, product_image_url) " +
            "VALUES(#{orderNumber}, #{addressesId}, #{userId}, #{productId}, #{productName}, " +
            "#{quantity}, #{productPrice}, #{totalPrice}, #{status}, #{createdAt}, #{recipientName}, #{phone}, #{address}, #{productImageUrl})")
    @Options(useGeneratedKeys = true, keyProperty = "ordersId")
    int insert(Order order);
    
    /**
     * 根据用户ID查询订单列表
     * @param userId 用户ID
     * @return 订单列表
     */
    @Select("SELECT * FROM orders WHERE user_id = #{userId} ORDER BY created_at DESC")
    List<Order> findByUserId(Integer userId);
    
    /**
     * 根据订单编号查询订单
     * @param orderNumber 订单编号
     * @return 订单对象
     */
    @Select("SELECT * FROM orders WHERE order_number = #{orderNumber}")
    Order findByOrderNumber(String orderNumber);
    
    /**
     * 更新订单状态
     * @param orderNumber 订单编号
     * @param status 订单状态
     * @return 影响的行数
     */
    @Update("UPDATE orders SET status = #{status} WHERE order_number = #{orderNumber}")
    int updateStatus(@Param("orderNumber") String orderNumber, @Param("status") String status);
    
    /**
     * 删除订单
     * @param orderNumber 订单编号
     * @param userId 用户ID
     * @return 影响的行数
     */
    @Delete("DELETE FROM orders WHERE order_number = #{orderNumber} AND user_id = #{userId}")
    int delete(@Param("orderNumber") String orderNumber, @Param("userId") Integer userId);
} 